<template>
  <div>
    <div class="Bread">
      <Bread></Bread>
    </div>
    <div class="search">
      <div class="title search_top">
        <div class="ei">
          <el-icon><Search /></el-icon>
          <h3>筛选搜索</h3>
        </div>
        <div>
          <el-button type="primary" @click="onSearch">查询搜索</el-button>
        </div>
      </div>
      <div class="search_input">
        <p>输入搜索:</p>
        <el-input type="text" placeholder="品牌名称/关键字" v-model="name"></el-input>
      </div>
    </div>
    <div class="adddata">
      <div class="title">
        <el-icon><Tickets /></el-icon>
        <h3>数据列表</h3>
      </div>

      <div>
        <el-button type="primary" @click="addProductCateFn">添加</el-button>
      </div>
    </div>
    <div class="tables">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        @select="onSelect"
        @select-all="onSelectAll"
      >
        <el-table-column width="60" align="center" type="selection"> </el-table-column>
        <el-table-column prop="id" label="编号" width="100" align="center" />
        <el-table-column prop="name" label="品牌名称" align="center" />
        <el-table-column
          prop="firstLetter"
          label="品牌首字母"
          width="100"
          align="center"
        />
        <el-table-column prop="sort" label="排序" width="100" align="center" />
        <el-table-column label="品牌制造商" width="100" align="center">
          <template #default="scope">
            <div>
              <el-switch
                :value="scope.row.factoryStatus === 1 ? true : false"
                @click="factoryStatusFn(scope.row.id, scope.row.factoryStatus)"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="是否显示" width="100" align="center">
          <template #default="scope">
            <div>
              <el-switch
                :value="scope.row.showStatus === 1 ? true : false"
                @click="ShowStatusFn(scope.row.id, scope.row.showStatus)"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="相关" width="220" align="center">
          <template #default="scope">
            <div class="text">
              <p>商品:<span>100</span></p>
              <p>评价:<span>1000</span></p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <div>
              <el-button @click="updateProductFn(scope.row.id)">编辑</el-button>
              <el-button type="danger" @click="deltProductFn(scope.row.id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="showgoods">
        <el-select v-model="showvalue" class="m-2" placeholder="批量操作">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="primary" @click="SelectAllFn"> 确定 </el-button>
      </div>
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 15, 20]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import * as api from "@/request/api";
import { ref } from "vue";
import type { Ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import Bread from "@/components/Bread.vue";
//路由跳转
const router = useRouter();
//列表数据
let tableData = ref([]) as Ref<any>;
//分页数据
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const small = ref(false);
const background = ref(false);
const disabled = ref(false);
// 搜索关键字
let name = ref("");
//进入页面发送请求
let getGoodsBrand = async () => {
  let data = await api.goods.GoodsBrandList({
    keyword: name.value,
    pageNum: currentPage.value,
    pageSize: pageSize.value,
  });
  // console.log(data);
  currentPage.value = data.pageNum;
  pageSize.value = data.pageSize;
  total.value = data.total;
  tableData.value = data.list;
};
getGoodsBrand();
let onSearch = async () => {
  // 发送请求,搜索数据
  getGoodsBrand();
};

//显示隐藏商品分类的数据
const showvalue = ref(null);
const options = [
  {
    value: 1,
    label: "显示品牌",
  },
  {
    value: 0,
    label: "隐藏品牌",
  },
];
//分页事件
const handleSizeChange = (val: number) => {
  getGoodsBrand();
};
const handleCurrentChange = (val: number) => {
  getGoodsBrand();
};
//添加逻辑
let addProductCateFn = () => {
  router.push({
    path: "/pms/addBrand",
  });
};
//编辑数据
let updateProductFn = (val: number) => {
  router.push({
    path: "/pms/updateBrand",
    // name: 'home',
    query: {
      id: val,
    },
  });
};
//删除逻辑
let deltProductFn = (val: number) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否删除该品牌", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      // console.log(val);

      await api.goods.GoodsDleteBrandList({
        id: val,
      });
      getGoodsBrand();
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};
//开关逻辑实现模块
let factoryStatusFn = async (val: number, val2: number) => {
  if (val2 == 1) {
    val2 = 0;
  } else if (val2 == 0) {
    val2 = 1;
  }
  let newData = await api.goods.updateFactoryStatus({
    ids: val,
    factoryStatus: val2,
  });
  if (newData) {
    ElMessage({
      message: "操作成功",
      grouping: true,
      type: "success",
    });
    getGoodsBrand();
  }
};
let ShowStatusFn = async (val: number, val2: number) => {
  if (val2 == 1) {
    val2 = 0;
  } else if (val2 == 0) {
    val2 = 1;
  }
  let newData = await api.goods.brandupdateshowStatus({
    ids: val,
    showStatus: val2,
  });
  if (newData) {
    ElMessage({
      message: "操作成功",
      grouping: true,
      type: "success",
    });
    getGoodsBrand();
  }
};
//单选全选显示隐藏
let ids = ref([]);
let onSelect = (e: any) => {
  // console.log(e);
  let newData = e.map((el) => {
    return el.id;
  });
  // console.log(newData);
  ids.value = newData;
  // console.log(ids.value);
};
let onSelectAll = (e: any) => {
  // console.log("onSelectAll", e);
  let newData = e.map((el) => {
    return el.id;
  });
  // console.log(newData);
  ids.value = newData;
  // console.log(ids.value);
};
//发送请求
let SelectAllFn = async () => {
  if (showvalue.value == null) {
    ElMessage({
      message: "请选取批量操作的类型",
      grouping: true,
      type: "warning",
    });
  } else if (ids.value.length == 0) {
    ElMessage({
      message: "请至少选择一条数据",
      grouping: true,
      type: "warning",
    });
  } else {
    let newData = await api.goods.brandupdateshowStatus({
      ids: ids.value,
      showStatus: showvalue.value,
    });
    if (newData) {
      ElMessage({
        message: "操作成功",
        grouping: true,
        type: "success",
      });
      getGoodsBrand();
    } else {
      ElMessage({
        grouping: true,
        message: "操作失败",
        type: "error",
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.search {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px 0;
  padding: 20px;
  flex-wrap: wrap;
  border: 1px solid #ebeef5;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
  .search_top {
    width: 100%;
  }
  .search_input {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 0 40px;
    padding-top: 20px;
    .el-input {
      width: 190px;
      height: 32px;
      font-size: 14px;
      padding: 0 15px;
    }
    p {
      width: 87px;
      height: 32px;
      line-height: 32px;
      font-size: 14px;
      color: #606266;
      padding: 0 12px 0 0;
      box-sizing: border-box;
    }
  }
}
.adddata {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  margin-bottom: 20px;
  border: 1px solid #ebeef5;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
}
.title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .ei {
    display: flex;
    align-items: center;
    h3 {
      color: #303133;
      font-size: 14px;
      margin-left: 10px;
    }
  }
  h3 {
    color: #303133;
    font-size: 14px;
    margin-left: 10px;
  }
}
.pagination {
  margin: 20px 0;
  padding-right: 20px;
  float: right;
}
.showgoods {
  margin: 20px 0;
  padding-right: 20px;
  float: left;
  .el-button {
    margin-left: 15px;
  }
}
.text {
  display: flex;
  justify-content: center;
  p {
    margin-left: 20px;
    font-size: 14px;
    span {
      font-size: 12px;
      color: rgb(64, 158, 255);
      cursor: pointer;
      &:hover {
        color: rgba(64, 158, 255, 0.6);
      }
      &:active {
        color: rgb(64, 158, 255);
      }
    }
  }
}
.Bread {
  position: sticky;
  top: -100px;
  margin: 0;
  width: 100%;
  border-bottom: 1px solid #eee;
  // box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
  padding-left: 20px;
  box-sizing: border-box;
}
</style>
